﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
using CSM.Controllers;
using CSM.Models;
using System.Text;

namespace CSM.Views.Wenjuan
{
    public partial class StaticsDetail : System.Web.UI.Page
    {
        CustomDataContext db = new CustomDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string statid=Request.QueryString["id"];
                string[] Qid = (from a in db.TCrm_WJ_Statistics_Detail where a.StatID == statid select a.QuestionID).Single().Split(',');
                var query = from b in db.TCrm_WJ_QuestionBase_Detail
                            where Qid.Contains(b.QuestionID)
                            group b by new {b.QuestionID,b.OptionID} into g
                            select new
                            {
                                QuestionID=g.Key.QuestionID,
                                QuestionName=(from c in db.TCrm_WJ_QuestionBase where c.QuestionID==g.Key.QuestionID select c.Title).Single().ToString(),
                                OptionID=g.Key.OptionID,
                                Title=(from c in db.TCrm_WJ_QuestionBase_Detail where c.QuestionID==g.Key.QuestionID &&c.OptionID==g.Key.OptionID select c.OptionLabel).Single().ToString(),
                                Num=(from c in db.TCrm_WJ_CustomerAnswer where c.StatID==statid &&c.QuestionID==g.Key.QuestionID && c.OptionID==g.Key.OptionID select c).Count(),
                                AllMumber = (from c in db.TCrm_WJ_CustomerAnswer where c.StatID == statid && c.QuestionID == g.Key.QuestionID select c).Count()
                            };
                GridView_Detail.DataSource = query;
                GridView_Detail.DataBind();
                GridViewUnit gv = new GridViewUnit();
                gv.UnitCell(GridView_Detail,0);
            }
        }
        public string Rate(string Num,string AllNumber)
        {
            double Nums = Convert.ToDouble(Num);
            double AllNumbers = Convert.ToDouble(AllNumber);
            double rate = 0;
            if (AllNumbers != 0)
            {
                rate = Nums / AllNumbers;
                if (rate == 0)
                {
                    return "0.00%";
                }
                else
                {
                    //return String.Format("{0:N2}", rate);
                    return string.Format("{0:#.##%}", rate);
                }
            }
            else
            {
                return "0.00%";
            }

        }

    }
}
